/**
 * LabelBgEditView.java [V 1.0.0]
 * Class : com.jiudaifu.yangsheng.view.LabelBgEditView
 *
 * Created by wangdong on 2014-10-22 下午3:27:59
 * Copyright (c) 2014-10-22 wangdong. All rights reserved.
 */
package com.example.apple.hxbtc.view;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.apple.hxbtc.R;


/**
 * com.jiudaifu.yangsheng.view.LabelBgEditView
 * @author wangdong
 *
 */
public class LabelBgEditView extends LinearLayout  implements View.OnClickListener, TextWatcher {
	
	private static final String NAMESPACE = "http://schemas.android.com/apk/res/android";

//    private TextView mTextView = null;
    private EditText mEditText = null;
    private ImageView mImage;
    private ImageView clearView;

    /*public LabelBgEditView(Context context) {
        this(context, null);
        initView();
    }*/

    public LabelBgEditView(Context context, AttributeSet attrs) {
        super(context, attrs);

        initView();
        readAttrs(attrs);
    }
    
    private void initView() {
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_bglab_edit, this, true);

//        mTextView = (TextView) findViewById(R.id.text_view);
        mEditText = (EditText) findViewById(R.id.edit_text);
        mEditText.addTextChangedListener(this);
        
        mImage = (ImageView) findViewById(R.id.image_view);
        
        clearView = (ImageView) findViewById(R.id.clear_button);
        clearView.setOnClickListener(this);
    }

    private void readAttrs(AttributeSet attrs) {
        if (attrs == null) {
            return;
        }
        String value;

//        value = attrs.getAttributeValue(NAMESPACE, "text");
//
//        if (value!=null && value.startsWith("@")) {
//            int id = Integer.valueOf(value.substring(1));
//            value = this.getContext().getString(id);
//        }
//
//        mTextView.setText(value);
        

        boolean b = attrs.getAttributeBooleanValue(NAMESPACE, "enabled", true);
        mEditText.setEnabled(b);

        value = attrs.getAttributeValue(NAMESPACE, "hint");

        if (value!=null && value.startsWith("@")) {
            int id = Integer.valueOf(value.substring(1));
            value = this.getContext().getString(id);
        }

        mEditText.setHint(value);

        //------------------------------
        value = attrs.getAttributeValue(NAMESPACE, "inputType");

        if (value!=null && value.startsWith("@")) {
            int id = Integer.valueOf(value.substring(1));
            value = this.getContext().getString(id);
        }

        if (value != null) {
            String x = value;
            int base = 10;

            if (x.startsWith("0x")) {
                x = x.substring(2);
                base = 16;
            }

            int type = Integer.valueOf(x, base);

            mEditText.setInputType(type);
        }

        //------------------------------
        value = attrs.getAttributeValue(NAMESPACE, "maxLength");

        if (value!=null && value.startsWith("@")) {
            int id = Integer.valueOf(value.substring(1));
            value = this.getContext().getString(id);
        }

        if (value != null) {
            String x = value;
            int base = 10;

            if (x.startsWith("0x")) {
                x = x.substring(2);
                base = 16;
            }

            int len = Integer.valueOf(x, base);

            mEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(len)});
        }
    }

    @Override
    public void onClick(View v) {
    	// wangdong
    	if(v.getId() == R.id.clear_button){
            mEditText.setText(null);
    	}
    }

    public void setEnabled(boolean b) {
        mEditText.setEnabled(b);
        findViewById(R.id.clear_button).setEnabled(b);
    }

    public void setText(String text) {
        mEditText.setText(text);
    }
    
    public void setSelection(int index) {
    	mEditText.setSelection(index);
    }
    
    public void setEditTextBackground(int drawable){
    	mEditText.setBackgroundResource(drawable);
    }
    
    public void setImagePadding(int left, int top, int right, int bottom) {
    	mImage.setPadding(left, top, right, bottom);
    }
    
    public void setImageBackgroundResource(int resid){
    	mImage.setBackgroundResource(resid);
    }
    
    public void setImageMargin(int left, int top, int right, int bottom){
    	RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
    	params.setMargins(left, top, right, bottom);//(0, 0,13, 0); 
    	mImage.setLayoutParams(params);
    }
    
    public void setClearImageMargin(int left, int top, int right, int bottom){
    	RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
    	params.setMargins(left, top, right, bottom);
    	clearView.setLayoutParams(params);
    }
    /*
    public void setLabPadding(int l, int r) {
    	mTextView.setPadding(l, -1, r, -1);
    }
    
    public void setLabColor(int c) {
        mTextView.setTextColor(c);
    }*/

    public String getText() {
        return mEditText.getText().toString();
    }

    public EditText getEditText() {
        return mEditText;
    }

    public void setEditBackground(int resId) {
        mEditText.setBackgroundResource(resId);
    }
    
    public void setEditTextPadding(int left, int top, int right, int bottom){
    	mEditText.setPadding(left, top, right, bottom);
    }
    
    public void setEditTextPaddingLeft(int left){
    	mEditText.setPadding(left, 0, 0, 0);
    }
    /**
     * 是否需要消除按钮
     * @param is
     */
    public void setHasClearView(boolean is) {
    	if (is) {
    		clearView.setVisibility(View.VISIBLE);
		}else {
			clearView.setVisibility(View.GONE);
		}
    }
    
    /**
     * int VISIBLE = 0x00000000;<br>
     * int INVISIBLE = 0x00000004;<br>
     * int GONE = 0x00000008;<br>
     * @param visible
     */
    public void setImageVisible(int visible){
    	mImage.setVisibility(visible);
    }
    
    public void setEditTextGravity(int gravity){
    	mEditText.setGravity(gravity);
    }

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		
	}

	@Override
	public void afterTextChanged(Editable s) {
		
	}
    
	/**
     * Sets the list of input filters that will be used if the buffer is
     * Editable. Has no effect otherwise.
     *	设置最大字符数
	 * @param max 最大字符数android.R.styleable#TextView_maxLength
     */
	public void setFilters(int max) {
		mEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(max)});
	}
    
}
